<?php
namespace App\Exports;

use Exception;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use App\Models\DriverWithdraw;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;

class WithdrawExport implements FromCollection, WithEvents ,WithStrictNullComparison
{
    private $row;
    private $data;
    public $rowNum;



    public function __construct($row,$data)
    {

        $this->row = $row;
        $this->data = $data;
    }

    /**
     * registerEvents.
     * 事件监听
     * @return array
     */
    public function registerEvents(): array
    {
        return [
            // 生成表单元后处理事件
            AfterSheet::class => function (AfterSheet $event) {
                // 设置单元格内容居中
//                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setVertical('center');
                //设置区域单元格水平居中
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setHorizontal('center');
                //设置字体大小
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getFont()->setSize(11);
                // 定义列宽度
                $widths = ['A' =>5, 'B' =>25, 'C' =>30, 'D' => 12, 'E' => 12, 'F' => 15, 'G' => 18, 'H' => 12,'I'=>30,'J'=>12,'K'=>18,'L'=>12,'M'=>12,'N'=>20];
                foreach ($widths as $k => $v) {
                    // 设置列宽度
                    $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
                }
                //设置行高，$i为数据行数
                for ($i = 0; $i <= $this->rowNum; $i++) {
                    $event->sheet->getDelegate()->getRowDimension($i)->setRowHeight(25);
                }
                // 设置表中字体
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->applyFromArray([
                    'font' => [
                        'name' => '宋体',
                        'bold' => false,
                        'italic' => false,
                        'strikethrough' => false,
                        'color' => [
                            'rgb' => '000000',
                        ],
                    ],
                ]);

            },
        ];

    }



    /**
     * collection.
     *
     * @return Collection
     *
     * @throws Exception
     */
    public function collection()
    {
        $row = $this->row;
        $data = $this->data;
        $parms = [];

        foreach ($data as $v){
            $parms[]=[
                'id'=>$v['id'],
                'withdraw_sn'=>"\t" .$v['withdraw_sn'],
                'created_at'=>$v['created_at'],
                'status'=>$v['status'],
                'money'=>$v['money'],
                'real_name'=>$v['real_name'],
                'phone'=>"\t" .$v['phone'],
                'admin'=>$v['admin'],
                'review_time'=>$v['review_time'],
                'type'=>$v['type'],
                'account'=>"\t" .$v['account'],
                'bank_name'=>$v['bank_name'],
                'account_name'=>$v['account_name'],
                'remit_sn'=>"\t" .$v['remit_sn'],
            ];
        }
        //设置表头
        foreach ($row[0] as $key => $value) {
            $key_arr[] = $key;
        }
        //输入数据
        foreach ($parms as $item) {
            array_push($row, (array)$item);
        }
        return collect($row);
    }
}
